package com.eastfair.venuebooking.controller;

import cn.hutool.core.util.StrUtil;
import com.eastfair.common.dto.AttachmentFileDTO;
import com.eastfair.common.vo.AttachmentFileVO;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.pay.api.AttachmentFileFeign;
import com.eastfair.venuebooking.enums.VenueBookingMoudleEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 附件 REST API.
 *
 * @author dqq
 * @date 2022 /6/23
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/attachmentFile")
public class AttachmentFileController {

    @Resource
    private AttachmentFileFeign attachmentFileFeign;

    /**
     * 查询附件列表.
     *
     * @param fileDTO the file dto
     * @return the r
     */
    @PostMapping("/query")
    public R<List<AttachmentFileVO>> queryAttachmentFile(@RequestBody AttachmentFileDTO fileDTO) {
        log.info("queryAttachmentFile - 查询附件列表, fileDTO={}", fileDTO);
        validateParam(fileDTO);

        R<List<AttachmentFileVO>> result = attachmentFileFeign.queryAttachmentFile(fileDTO);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        List<AttachmentFileVO> list = result.getData();
        return R.successDef(list);
    }

    /**
     * 保存附件信息.
     *
     * @param fileDTOs the file dt os
     * @return the r
     */
    @PostMapping("/save")
    public R<Boolean> saveAttachmentFile(@RequestBody List<AttachmentFileDTO> fileDTOs) {
        log.info("saveAttachmentFile - 保存附件, fileDTOs={}", fileDTOs);
        fileDTOs.forEach(this::validateParam);
        R<?> result = attachmentFileFeign.addAttachmentFile(fileDTOs);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return R.successDef(true);
    }

    /**
     * 移除附件.
     *
     * @param ids the ids
     * @return the r
     */
    @PostMapping("/remove")
    public R<Boolean> removeAttachmentFile(@RequestBody List<Long> ids) {
        log.info("removeAttachmentFile - 移除附件, ids={}", ids);
        R<?> result = attachmentFileFeign.deteleBatch(ids);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return R.successDef(true);
    }

    /**
     * 校验参数
     *
     * @param fileDTO AttachmentFileDTO
     * @throws BizException BizException
     */
    private void validateParam(AttachmentFileDTO fileDTO) throws BizException {
        if (StrUtil.isBlank(fileDTO.getBusinessType())) {
            throw BizException.validFail("业务类型不可为空");
        }
        boolean businessTypeParamError = true;
        for (VenueBookingMoudleEnum venueBookingMoudleEnum : VenueBookingMoudleEnum.values()) {
            if (venueBookingMoudleEnum.getCode().equals(fileDTO.getBusinessType())) {
                businessTypeParamError = false;
                break;
            }
        }
        if (businessTypeParamError) {
            throw BizException.validFail("业务类型参数错误");
        }
    }
}
